package gatecontrol.network.http;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.xkh.xkh.MyApplication;
import managers.VolleySingleton;
import shuiyuqing.network.beans.IrrBMSystem;
import shuiyuqing.network.beans.RealTimeIrrBRWaterDam;
import shuiyuqing.network.http.DownloadInfo;


/**
 * 闸坝实时水情信息
 * Created by 凯 on 2015/11/6.
 */
public class GetRealWaterGateDam {

    // 上行数据
    String stadiacode;

    // 下行数据
    Download download;

    Delegate delegate;

    public GetRealWaterGateDam(String stadiacode) {
        this.stadiacode = stadiacode;
    }

    public String getStadiacode() {
        return stadiacode;
    }

    public void setStadiacode(String stadiacode) {
        this.stadiacode = stadiacode;
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }

    public Delegate getDelegate() {
        return delegate;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    // 获取基本的信息
    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/GetRealWaterGateDam";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        afterGet(response);
                        if (delegate != null)
                            delegate.onResponse(response,GetRealWaterGateDam.this);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error,GetRealWaterGateDam.this);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("data",stadiacode);
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 获取下行数据后
    private void afterGet(String response) {
        download = JSON.parseObject(response,Download.class);
        if (download.getStatus() == DownloadInfo.SUCCESS)
        {

        }else if (download.getStatus() == DownloadInfo.FAILED)
        {

        }

    }


    // 下行数据
    public static class Download
    {

        int status;
        // 最新实时数据
        RealTimeIrrBRWaterDam realTimeIrrBRWaterDam;

        // 最新照片
        IrrBMSystem irrBMSystem;



        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public RealTimeIrrBRWaterDam getRealTimeIrrBRWaterDam() {
            return realTimeIrrBRWaterDam;
        }

        public void setRealTimeIrrBRWaterDam(RealTimeIrrBRWaterDam realTimeIrrBRWaterDam) {
            this.realTimeIrrBRWaterDam = realTimeIrrBRWaterDam;
        }

        public IrrBMSystem getIrrBMSystem() {
            return irrBMSystem;
        }

        public void setIrrBMSystem(IrrBMSystem irrBMSystem) {
            this.irrBMSystem = irrBMSystem;
        }




    }

    public static interface Delegate
    {
        public void onResponse(String response, GetRealWaterGateDam dam);
        public void onErrorResponse(VolleyError error, GetRealWaterGateDam dam);
    }

}

